package com.simpleframe.util;

import org.apache.commons.lang.StringUtils;

public class StringUtil {
	
	public static boolean hasLength(CharSequence str) {
		return (str != null && str.length() > 0);
	}

	public static boolean hasLength(String str) {
		return hasLength((CharSequence) str);
	}
	
	public static boolean containChar(String str, String chr){
		if(hasLength(str))
			return str.indexOf(chr) != -1;
		return false;
	}
	
	public static String trim(String str){
		if(str==null){
			return "";
		}
		return str.trim();
	}
	
	public static String convertEmpty(String str){
		if(str==null){
			str = "";
		}
		return str;
	}
	
	/**
	 * 过滤特殊字符
	 * @author guojt
	 * @param content 要过滤的内容
	 * @return
	 */
	public static String filter(String content) {
		if (content == null) {
			content = "";
			return content;
		}
		content = content.trim().replaceAll("&", "&amp;");
		content = content.trim().replaceAll("<", "&lt;");
		content = content.trim().replaceAll(">", "&gt;");
		content = content.trim().replaceAll("\t", "    ");
		content = content.trim().replaceAll("\r\n", "\n");
		content = content.trim().replaceAll("\n", "<br/>");
		content = content.trim().replaceAll("  ", " &nbsp;");
		content = content.trim().replaceAll("'", "&#39;");
		content = content.trim().replaceAll("\\\\", "&#92;");
		return content;
	}
	
	/**
	 * 检查输入的字符是否为数字或者英文
	 * @param str
	 * @return
	 */
	public static boolean isCharOrNumber(String str){
		if(StringUtils.isEmpty(str)){
			return false;
		}
		for (int i = 0; i < str.length(); i++) {
			if(str.charAt(i)<'0'||str.charAt(i)>'9'){
				if(str.charAt(i)<'a'||str.charAt(i)>'z'){
					if(str.charAt(i)<'A'||str.charAt(i)>'Z'){
						return false;
					}
				}
			}
		}
		return true;
	}
	
	public static String  toFirstUpperCase(String str){
		//Assert.notEmpty(str);
		if(hasLength(str)){
			return str.substring(0,1).toUpperCase()+ str.substring(1);
		}
		return "";
	}
	
	public static String toFirstLowerCase(String str){
		if(hasLength(str)){
			return str.substring(0,1).toLowerCase()+ str.substring(1);
		}
		return "";
	}
	
	public static String delete(String inString, String pattern) {
		return replace(inString, pattern, "");
	}

	public static String deleteAny(String inString, String charsToDelete) {
		if (!hasLength(inString) || !hasLength(charsToDelete)) {
			return inString;
		}
		StringBuffer out = new StringBuffer();
		for (int i = 0; i < inString.length(); i++) {
			char c = inString.charAt(i);
			if (charsToDelete.indexOf(c) == -1) {
				out.append(c);
			}
		}
		return out.toString();
	}
	
	public static String replace(String inString, String oldPattern, String newPattern) {
		if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) {
			return inString;
		}
		StringBuffer sbuf = new StringBuffer();
		// output StringBuffer we'll build up
		int pos = 0; // our position in the old string
		int index = inString.indexOf(oldPattern);
		// the index of an occurrence we've found, or -1
		int patLen = oldPattern.length();
		while (index >= 0) {
			sbuf.append(inString.substring(pos, index));
			sbuf.append(newPattern);
			pos = index + patLen;
			index = inString.indexOf(oldPattern, pos);
		}
		sbuf.append(inString.substring(pos));
		// remember to append any characters to the right of a match
		return sbuf.toString();
	}
	
	/**
     * 
     * @param source
     * @param len
     * @param delim
     * @return
     */
	public static final Object truncate(String str, int toCount, String more) {
		int reInt = 0;
	      String reStr = "";
	      if (str == null)
	        return "";
	      char[] tempChar = str.toCharArray();
	      for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) {
	        String s1 = str.valueOf(tempChar[kk]);
	        byte[] b = s1.getBytes();
	        reInt += b.length;
	        reStr += tempChar[kk];
	      }
	      if (toCount == reInt || (toCount == reInt - 1))
	        reStr += more;
	      return reStr;
	}
	
	public static String formatIds(Long[] ids) {
		// 安全检查
		if (ids == null) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < ids.length; i++) {
			if (i != ids.length - 1) {
				sb.append(ids[i] + ",");
			} else {
				sb.append(ids[i] + " ");
			}
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		String name = "bbbbxccc";
		System.out.println(StringUtil.containChar(name, "x"));
	}
}
